//
//  YCLogger.swift
//  YCNetworking
//
//  Created by YC on 2017/3/8.
//  Copyright © 2017年 YC. All rights reserved. mail:yangchong364@qq.com
//

import UIKit

class YCLogger {
    static let shared = YCLogger()
    private init() {}
    
    class func logDebugInfo(with request: URLRequest, apiName: String, service: YCService, requestParams: Dictionary<String,Any>, httpMethod: String) {
        #if DEBUG
            var logString = "*************************************************************\n*                       Request Start                       *\n*************************************************************"
            
            logString.append("API Name:\t\t\(apiName)\n")
            logString.append("Method:\t\t\(httpMethod)\n")
            logString.append("API Controller:\t\t\(service.apiController)\n")
            logString.append("Service:\t\t\(service)\n")
            logString.append("Params:\t\t\(requestParams)\n")
            logString.append("\n\nHTTP URL:\n\t\(String(describing: request.url))")
            logString.append("\n\nHTTP Header:\n\(String(describing: request.allHTTPHeaderFields))")
            logString.append("\n\nHTTP Body:\n\t\(String(describing: String(data: request.httpBody!, encoding: .utf8)))\n\n")
            logString.append("*************************************************************\n*                         Request End                       *\n*************************************************************\n\n\n\n")
            
            NSLog(logString)
            
        #endif
    }
    
    class func logDebugInfo(with response: HTTPURLResponse?, responseString: String, request: URLRequest, error: NSError?) {
        #if DEBUG
            var logString = "=============================================================\n=                        API Response                       =\n============================================================="
            logString.append("\n---------------  Related Request Content  --------------\n")
            logString.append("\n\nHTTP URL:\n\t\(String(describing: request.url))")
            logString.append("\n\nHTTP Header:\n\(String(describing: request.allHTTPHeaderFields))")
            logString.append("\n\nHTTP Body:\n\t\(String(describing: String(data: request.httpBody!, encoding: .utf8)))\n\n")
            logString.append("Content:\n\t\(responseString)\n\n")
            
            if response != nil {
                logString.append("Status:\t\(response!.statusCode)\t(\(HTTPURLResponse.localizedString(forStatusCode: response!.statusCode)))\n\n")
            }

            if (error != nil) {
                logString.append("Error Domain:\t\t\t\t\t\t\t\(error!.domain)\n")
                logString.append("Error Domain Code:\t\t\t\t\t\t\(error!.code)\n")
                logString.append("Error Localized Description:\t\t\t\(error!.localizedDescription)\n")
                logString.append("Error Localized Failure Reason:\t\t\t\(String(describing: error!.localizedFailureReason))\n")
                logString.append("Error Localized Recovery Suggestion:\t\(String(describing: error!.localizedRecoverySuggestion))\n\n")
                
                logString.append("=============================================================\n=                        Response End                       =\n=============================================================")
            }
            
            NSLog(logString)
            
        #endif
    }
    
    class func logDebugInfo(with cachedResponse: YCURLResponse, methodName: String, service: YCService) {
        #if DEBUG
            var logString = "=============================================================\n=                      Cached Response                      =\n============================================================="
            logString.append("API Name:\t\t\(methodName)\n")
            logString.append("API Controller:\t\t\(service.apiController)\n")
            logString.append("Service:\t\t\(service)\n")
            logString.append("Method Name:\t\(methodName)\n")
            logString.append("Params:\n\(cachedResponse.responseData)\n\n")
            logString.append("Content:\n\t\(cachedResponse.contentString)\n\n")
            
            logString.append("=============================================================\n=                        Response End                       =\n=============================================================")
            
            NSLog(logString)
            
        #endif
    }
    
}
